<?php

namespace app\controller\admin\cnf;

use app\bll\Datadict as BllDatadict;
use core\controller\AdminBase;
use Exception;
use support\View;

class Datadict extends AdminBase
{
    public function index()
    {

        $bllDatadict = new BllDatadict();
        $dicts       = $bllDatadict->paginator();
        View::assign('dicts', $dicts);
        return $this->view();
    }

    public function add()
    {
        if (is_post()) {
            try {
                $bllDatadict = new BllDatadict();
                $data = request()->post();
                $data['values'] = array_values($data['values']);
                $bllDatadict->add($data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            return $this->view(frame: false);
        }
    }

    /**
     * 编辑
     *
     * @return mixed
     */
    public function edit()
    {
        $bllDatadict = new BllDatadict();
        $id = request()->get('id');
        if (is_post()) {
            try {
                $bllDatadict = new BllDatadict();
                $data = request()->post();
                $data['values'] = array_values($data['values']);
                $bllDatadict->edit($id, $data);
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $dictinfo = $bllDatadict->find($id);
            View::assign('dictinfo', $dictinfo);
            return $this->view(frame: false);
        }
    }


    /**
     * 删除
     *
     * @return mixed
     */
    public function delete()
    {
        $id = request()->get('id');
        try {
            $bllDatadict = new BllDatadict();
            $bllDatadict->delete($id);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
